<?php
App::import('Controller', 'TimeLines');
App::import('Controller', 'Photos');
class MomentsController extends AppController
{
   
    
    
    
    public function index()
    {
        $this->layout = 'home';
    }
    
    public function view($id = null)
    {
        
        $this->layout = 'home';
        if($id)
        {
            if($this->Moment->findById($id))
                $this->set('moment', $this->Moment->findById($id));
            if (isset($this->params['requested']))
                return $this->Moment->findById($id);
                
        }else{
            $this->set('moment', $this->Moment->find($id));
        }
    }
    
    public function delete($id = null)
    {
        
        $this->layout = 'home';
        if($id)
        {
            if($this->Moment->delete($id, true))    
                    return 1;
            else
                    return 0;
        }else{
            return 0;
        }
    }
    
    
    
    public function add()
    {
        $TimeLines = new TimeLinesController;
        $TimeLines->constructClasses();
        $Photos = new PhotosController;
        $Photos->constructClasses();
        $this->loadModel('TimeLine');
        $this->loadModel('Photo');
        $this->loadModel('Message');
        
        $ultimoTimeline = $TimeLines->last();
        
        if($this->data['temporize']['Photo']['size'] <= 2097152 && $this->data['temporize']['Photo']['type'] == 'image/jpeg')
        {
            $id = $ultimoTimeline[0][0]['id'];
            $ultimadata = getdate(strtotime($ultimoTimeline[0][0]['create_date']));
            $agora = getdate(time());
            if((mktime(0, 0, 0, $agora['mon'], $agora['mday'], $agora['year']) - 
                    mktime(0, 0, 0, $ultimadata['mon'], $ultimadata['mday'], $ultimadata['year'])) >= 86400)
            {
                $this->requestAction('/time_lines/create');
                $id = $this->TimeLine->getInsertId();
            }

            $data = array(
              "title"  => $this->data['Moment']['title'],
              "description" => $this->data['Moment']['description'],
              "time_line_id" => $id,
              'create_hour' => date(DATE_ATOM,time()),
            );

            $this->set('teste', $this->data);
            $this->Moment->save($data);
            $Photos->add($this->data['temporize'], $this->Moment->getInsertId());
                $message = array(
                  'message' => 'Moment inserted successfully',
                    'user_id' => $this->Auth->User('id'),
                    'status' => 1,
                );
        }else{
            $message = array(
                  'message' => 'Could not insert the moment',
                    'user_id' => $this->Auth->User('id'),
                'status' => 0,
            );
            
        }
        $this->Message->save($message);
        $this->redirect('/users/');
    }
    
    function viewThumb($id = null)
    {
        if($id)
            return $this->Moment->findById($id);
    }
}
?>
